package net.fdukedom.epicurus.pm.actions;

import net.fdukedom.epicurus.presentation.actions.AbstractAction;
import net.fdukedom.epicurus.presentation.SessionData;
import net.fdukedom.epicurus.domain.entity.User;
import net.fdukedom.epicurus.domain.util.UserHelper;
import net.fdukedom.epicurus.pm.domain.entity.PrivateMessage;
import net.fdukedom.epicurus.pm.domain.util.PrivateMessageHelper;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Register action.
 *
 * @author Dmitry Y. Kochelaev
 * @author Sergey Vishnyakov
 */
public class SendAction extends AbstractAction<ComposeForm> {

    private PrivateMessageHelper privateMessageHelper = PrivateMessageHelper.getInstance();

    protected ActionForward doExecute(ActionMapping mapping, ComposeForm form, HttpServletRequest request, HttpServletResponse response) {
        User user = SessionData.getInstance(request).getUser();
        PrivateMessage message = new PrivateMessage();
        message.setFrom(user);
        message.setTo(UserHelper.getInstance().findByName(form.getTo()));
        message.setIsRead(false);
        message.setTitle(form.getTitle());
        message.setBody(form.getBody());
        privateMessageHelper.savePrivateMessage(message);
        return mapping.findForward("success");
    }
}
